package com.hxh.basic.project.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.HashMap;

public class JwtTokenUtils {

    //public static final String TOKEN_HEADER = "Authorization";
    //public static final String TOKEN_PREFIX = "Bearer ";

    public static final String TOKEN_HEADER = "DICToken";
    public static final String TOKEN_PREFIX = "";
    //秘钥
    private static final String SECRET = "cn.unicom.club";
    //签发者
    private static final String ISS = "unicomClub";

    // 角色的key
    private static final String ROLE_CLAIMS = "role";
    private static final String ROLE_LEVEL = "roleLevel";
    private static final String ROLE_ID = "roleId";
    private static final String USER_ID = "userId";
    private static final String NICK_NAME = "nickName";
    private static final String DEPT_CODE = "deptId";
    private static final String PASSWORD = "password";
    private static final String USERNAME = "username";
    // 过期时间是3600秒，既是1个小时
    private static final long EXPIRATION = 7200L;

    // 选择了记住我之后的过期时间为7天
    private static final long EXPIRATION_REMEMBER = 604800L;


    // 创建token
    public static String createToken(String username,String password) {
//        long expiration = isRememberMe ? EXPIRATION_REMEMBER : EXPIRATION;
        HashMap<String, Object> map = new HashMap<>();
        map.put(USERNAME,username);
        map.put(PASSWORD,password);
        Date IssuedAt = new Date();
        Date Expiration = new Date(System.currentTimeMillis() + EXPIRATION * 1000);
        String jwt = Jwts.builder()
                .signWith(SignatureAlgorithm.HS512, SECRET)
                .setClaims(map)
                .setIssuer(ISS)
                .setSubject(username)
                .setIssuedAt(IssuedAt)
                .setExpiration(Expiration)
                .compact();
        return jwt;
    }

    // 创建token
    public static String createOauthToken(String username) {
        HashMap<String, Object> map = new HashMap<>();
        map.put(USERNAME,username);
        Date IssuedAt = new Date();
        Date Expiration = new Date(System.currentTimeMillis() + EXPIRATION * 1000);
        String jwt = Jwts.builder()
                .signWith(SignatureAlgorithm.HS512, SECRET)
                .setClaims(map)
                .setIssuer(ISS)
                .setSubject(username)
                .setIssuedAt(IssuedAt)
                .setExpiration(Expiration)
                .compact();
        return jwt;
    }

    public static String createLoginToken(String username,String password) {
        Date IssuedAt = new Date();
        Date Expiration = new Date(System.currentTimeMillis());
        HashMap<String, Object> map = new HashMap<>();
        map.put(PASSWORD, password);
        String jwt = Jwts.builder()
                .signWith(SignatureAlgorithm.HS512, SECRET)
                .setClaims(map)
                .setIssuer(ISS)
                .setSubject(username)
                .setIssuedAt(IssuedAt)
                .setExpiration(Expiration)
                .compact();
        return jwt;
    }

    // 从token中获取用户名(ps:zouyujie)
    public static String getUsername(String token) {
        return getTokenBody(token).getSubject();
    }

    // 从token中获取用户姓名(ps:邹宇杰)
    public static String getNickName(String token) {
        return (String) getTokenBody(token).get(NICK_NAME);
    }

    // 获取用户角色
    public static String getUserRole(String token) {
        return (String) getTokenBody(token).get(ROLE_CLAIMS);
    }

    //获取密码
    public static String getPassword(String token) {
        return (String) getTokenBody(token).get(PASSWORD);
    }

    //获取用户id
    public static Integer getUserId(String token) {return (Integer) getTokenBody(token).get(USER_ID);}

    // 获取用户角色级别
    public static Integer getUserRoleLevel(String token) {
        return (Integer) getTokenBody(token).get(ROLE_LEVEL);
    }

    // 获取用户部门
    public static Integer getDeptCode(String token) {
        return Integer.parseInt((String) getTokenBody(token).get(DEPT_CODE));
    }

    //获取用户角色Id
    public static Integer getUserRoleId(String token) {
        return (Integer) getTokenBody(token).get(ROLE_ID);
    }

    // 获取过期时间
    public static Date getIssuedAt(String token) {
        return getTokenBody(token).getIssuedAt();
    }

    // 获取过期时间
    public static Date getExpiration(String token) {
        return getTokenBody(token).getExpiration();
    }

    // 是否已过期
    public static boolean isExpiration(String token) {
        return getTokenBody(token).getExpiration().before(new Date());
    }


    private static Claims getTokenBody(String token) {
        Claims claims;
        try {
            claims = Jwts.parser()
                    .setSigningKey(SECRET).parseClaimsJws(token)
                    .getBody();
        } catch (ExpiredJwtException e) {
            claims = e.getClaims();
        }
        return claims;
    }
}
